package cn.wom.core.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import cn.wom.core.bean.MissionOrder;
import cn.wom.core.bean.QueryPojo;
import cn.wom.core.bean.User;
import cn.wom.core.bean.WorkOrder;
import cn.wom.core.dao.OrderMapper;
import cn.wom.core.service.OrderService;

@Controller
@SessionAttributes({ "user_login", "user_login" })
public class OrderController {

	@Autowired
	private OrderService order;

	@Autowired
	private OrderMapper orderMapper;
	
	
	@RequestMapping("/warning")
	@ResponseBody
	public String warning() {
		return null;
	}

	@RequestMapping("/login")
	@ResponseBody
	public String login(User user, Model model, HttpSession session) {
		
		User user1 = orderMapper.selectUserByUsername(user.getUsername());
		if (session.getAttribute("user_login") == null) {
			if (user1 != null) {
				if (!user1.getState().equals("locked")) {
					if (user1.getPassword().equals(user.getPassword())) {
						model.addAttribute("user_login", user1);
						return "ok";
					} else {
						return "no";
					}
				}
				return "locked";
			}
			return "no";
		}
		return "repetition";
	}

	@RequestMapping(value = "/index")
	public String showCumtomer() {
		return "adminIndex";
	}

	@RequestMapping(value = "/queryWorkOrder")
	public String showWorkOrder() {
		return "workOrder";
	}

	@RequestMapping(value = "/queryWorkOrderList")
	public String showWorkOrderList(QueryPojo q_pojo, Model model) {
		List<WorkOrder> workOrderList = order.selectWorkOrderList(q_pojo);
		model.addAttribute("q_pojo", q_pojo);
		model.addAttribute("w_list", workOrderList);

		return "workOrder";
	}

	// 添加明细表
	@RequestMapping(value = "/createWorkOrder")
	public String createWorkOrder(String m_id, Model model, HttpSession sess) {
		MissionOrder missionOrder = order.selectMissionOrder(m_id);
		String w_id = m_id + "WO" + (missionOrder.getChild_count() + 1);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = simpleDateFormat.format(new Date());
		simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
		String time = simpleDateFormat.format(new Date());
		User user_login = (User) sess.getAttribute("user_login");
		model.addAttribute("w_id", w_id);
		model.addAttribute("m_order", missionOrder);
		model.addAttribute("w_user", user_login.getUsername());
		model.addAttribute("w_date", date);
		model.addAttribute("w_time", time);

		return "createWorkOrder";
	}

	@RequestMapping(value = "/editWorkOrder")
	public String editWorkOrder(String w_id, String m_id, Model model) {
		WorkOrder w_Order = order.selectWorkOrder(w_id);
		MissionOrder m_Order = order.selectMissionOrder(m_id);
		model.addAttribute("w_order", w_Order);
		model.addAttribute("m_order", m_Order);

		return "editWorkOrder";
	}

	@RequestMapping(value = "/updateWorkOrder")
	@ResponseBody
	public String updateWorkOrder(WorkOrder w_order, HttpSession sess) {
		WorkOrder workOrder = order.selectWorkOrder(w_order.getW_id());
		String audit = workOrder.getAudit();
		User user_login = (User) sess.getAttribute("user_login");
		boolean b = false;
		if (user_login.getRole().equals("manager")) {
			b = order.updateWorkOrderAndUpdateChild(w_order);
			if (b) {
				return "ok";
			} else {
				return "no";
			}
		}
		if (audit.equals("no")) {
			b = order.updateWorkOrderAndUpdateChild(w_order);

			if (b) {
				return "ok";
			} else {
				return "no";
			}
		} else {
			return "forbid";
		}
	}

	@RequestMapping(value = "/saveWorkOrder")
	@ResponseBody
	public String saveWorkOrder(WorkOrder w_order, Model model) {
		// System.out.println(w_order);
		boolean b = order.insertWorkOrderAndUpdateChild(w_order);
		if (b) {
			return "ok";
		} else {
			return "no";
		}
	}

	@RequestMapping(value = "/deleteWorkOrder")
	@ResponseBody
	public String deleteWorkOrder(String m_id, String w_id, HttpSession sess) {
		WorkOrder workOrder = order.selectWorkOrder(w_id);
		String audit = workOrder.getAudit();
		User user_login = (User) sess.getAttribute("user_login");
		boolean b = false;
		if (user_login.getRole().equals("manager")) {
			b = order.deleteWorkOrder(m_id, w_id);
			if (b) {
				return "ok";
			} else {
				return "no";
			}
		}
		if (audit.equals("no")) {
			b = order.deleteWorkOrder(m_id, w_id);
			if (b) {
				return "ok";
			} else {
				return "no";
			}
		} else {
			return "forbid";
		}
	}

	@RequestMapping(value = "/registerPage")
	@ResponseBody
	public String registerPage(User user) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = simpleDateFormat.format(new Date());
		user.setCreate_date(date);
		boolean flag = order.register(user);
		if (flag) {
			return "ok";
		} else {
			return "no";
		}
	}

	@RequestMapping(value = "/register")
	public String register() {
		return "register";
	}

	@RequestMapping(value = "/queryUser")
	public String queryUser(Model model) {
		List<User> list = orderMapper.queryUser();
		model.addAttribute("list", list);
		return "queryUser";
	}

	@RequestMapping(value = "/editUser")
	@ResponseBody
	public User editUser(Model model, String id) {
		User user = orderMapper.editUser(Integer.parseInt(id));
		return user;
	}

	@RequestMapping(value = "/updateUser")
	@ResponseBody
	public String updateUser(Model model, User user) {
		orderMapper.updateUser(user);
		return "OK";
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public String customerDelete(String id) {
		orderMapper.deleteUser(Integer.parseInt(id));
		return "OK";
	}

	@RequestMapping(value = "/quit")
	public String quit(HttpSession session, SessionStatus sessionStatus) {
		session.removeAttribute("user_login");
		sessionStatus.setComplete();
		return "redirect:/login.jsp";
	}

	@RequestMapping(value = "/queryMissionOrder")
	public String queryMissionOrder() {
		return "queryMissionOrder";
	}

	@RequestMapping(value = "/queryMissionOrderList")
	public String queryMissionOrderList(QueryPojo pojo, Model model) {
		List<MissionOrder> MO_list = orderMapper.selectMissionOrderList(pojo);

		model.addAttribute("q_pojo", pojo);
		model.addAttribute("MO_list", MO_list);
		return "queryMissionOrder";
	}

	@RequestMapping(value = "/editMissionOrder")
	public String editMissionOrder(String m_id, Model model) {
		MissionOrder m_order = orderMapper.selectMissionOrder(m_id);
		model.addAttribute("m_order", m_order);
		return "editMissionOrder";
	}

	@RequestMapping(value = "/updateMissionOrder")
	@ResponseBody
	public String updateMissionOrder(MissionOrder m_order) {
		int row = orderMapper.updateMissionOrder(m_order);
		if (row > 0) {
			return "ok";
		} else {
			return "no";
		}
	}

	@RequestMapping(value = "/addMissionOrder")
	public String addMissionOrder() {

		return "addMissionOrder";
	}

	@RequestMapping(value = "/saveMission1")
	@ResponseBody
	public String saveMission(MissionOrder m_order, HttpSession session) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
		String date = simpleDateFormat.format(new Date());
		String m_id = "MO" + date;
		m_order.setM_id(m_id);
		System.out.println(m_order.toString());
		simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
		String m_date = simpleDateFormat.format(new Date());
		String m_time = simpleDateFormat1.format(new Date());
		m_order.setM_date(m_date);
		m_order.setM_time(m_time);
		User user_login = (User) session.getAttribute("user_login");
		m_order.setM_user(user_login.getUsername());
		boolean flag = order.saveMissionOrder(m_order);
		if (flag) {
			return "ok";
		} else {
			return "no";
		}

	}

	@RequestMapping(value = "/deleteMissionOrder")
	@ResponseBody
	public String deleteMissionOrder(String m_id) {

		boolean b = order.deleteMissionOrder(m_id);
		if (b) {
			return "ok";
		} else {
			return "no";
		}

	}
	
	@RequestMapping(value = "/statistics")
	public String statistics() {
		return "statistics";
	}
	
	@RequestMapping(value = "/queryStatistics")
	public String queryStatistics(QueryPojo pojo,Model model) {
		/*QueryPojo pojo = new QueryPojo();
		Map<String, String[]> parameterMap = request.getParameterMap();
		try {
			BeanUtils.populate(pojo, parameterMap);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(pojo);*/
		WorkOrder w_order =new WorkOrder ();
		List<WorkOrder> workOrderList = order.selectWorkOrderList(pojo);
		int defective_count =0;
		int good_count =0;
		double true_weight =0;
		double defective_weight =0;
		double good_weight =0;
		double w_weight =0;
		
		for(WorkOrder wo : workOrderList){
			defective_count += wo.getDefective_count();
			good_count += wo.getGood_count();
			true_weight += wo.getTrue_weight();
			good_weight += wo.getGood_weight();
			defective_weight += wo.getDefective_weight();
			w_weight += wo.getW_weight();
		}
		w_order.setW_weight(w_weight);
		w_order.setGood_count(good_count);
		w_order.setGood_weight(good_weight);
		w_order.setDefective_count(defective_count);
		w_order.setDefective_weight(defective_weight);
		w_order.setTrue_weight(true_weight);
		model.addAttribute("q_pojo", pojo);
		model.addAttribute("w_order", w_order);
		
		return "statistics";
	}
	
	
	
}